//
//  ButtonExtension.swift
//  WineComing
//
//  Created by xiaozhi on 15/8/13.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
extension  UIButton{
    func addCount()->Void{
        var totalTime:NSTimeInterval = 60.0
        let lbl:UILabel = UILabel(frame: self.bounds)
        lbl.backgroundColor = self.backgroundColor
        lbl.textAlignment = NSTextAlignment.Center
        lbl.textColor = self.titleLabel?.textColor
        lbl.font = UIFont.systemFontOfSize(12.0)
        self.addSubview(lbl)
        let queue:dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        var timer:dispatch_source_t? = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)
        dispatch_source_set_timer(timer!, dispatch_walltime(nil, 0), NSEC_PER_SEC*UInt64(1.0), 0)
        dispatch_source_set_event_handler(timer!, {[weak self] in
            if totalTime<=0{
                dispatch_source_cancel(timer!)
                timer = nil
                dispatch_async(dispatch_get_main_queue()){
                    self?.enabled = true
                    lbl.removeFromSuperview()
                }
            }
            else{
                dispatch_async(dispatch_get_main_queue()){
                self?.enabled = false
                 let text = NSString(format: "重新获取(%2d)",Int(totalTime)) as String
                    lbl.text = text
                }
            }
            totalTime--
            })
        dispatch_resume(timer!)
    }
}
class TabButton:UIButton{
    var brageLabel:UILabel!
    var imageRatio:CGFloat = 0.8
    var alwaysUnhightlighted = true
    var brageNumber:NSInteger = 0{
        didSet{
            brageLabel.text = "\(brageNumber)"
            if brageNumber >= 100{
                brageLabel.bounds = CGRectMake(0, 0, 24, 16)
            }
            else{
                brageLabel.bounds = CGRectMake(0, 0, 16, 16)
            }
            brageLabel.hidden = brageNumber==0
        }
    }
     override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        return CGRectMake(0, contentRect.size.height*imageRatio-5, contentRect.size.width, contentRect.size.height-contentRect.size.height*imageRatio)
    }
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        return CGRectMake(0, 0, contentRect.size.width, contentRect.size.height*imageRatio)
    }
    func showUnlight(button:UIButton){
        if alwaysUnhightlighted{
            self.highlighted = false
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addTarget(self, action: "showUnlight:", forControlEvents: UIControlEvents.AllTouchEvents)
        brageLabel = UILabel(frame: CGRectMake(0,0, 16, 16))
        brageLabel.center = CGPointMake(bounds.size.width/2.0+16, bounds.size.height/2.0-16)
        brageLabel.backgroundColor = commonRedColor
        brageLabel.textColor = UIColor.whiteColor()
        brageLabel.textAlignment = NSTextAlignment.Center
        brageLabel.clipsToBounds = true
        brageLabel.layer.cornerRadius = 8.0
        brageLabel.font = UIFont.boldSystemFontOfSize(11)
        brageLabel.hidden = true
        self.addSubview(brageLabel)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
class CommonButton:UIButton{
    var imageRatio:CGFloat = 0.8
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        return CGRectMake(self.bounds.size.width*2/3-40, 0, 40,self.bounds.size.height)
    }
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        return CGRectMake(self.bounds.size.width*2/3-10, (self.bounds.size.height - 20)/2.0, 30, 20)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
class ImageHeadButton:UIButton{
    var imageSize:CGSize = CGSizeZero
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        return CGRectMake(imageSize.width+5, 0, self.bounds.size.width-imageSize.width-5,self.bounds.size.height)
    }
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        return CGRectMake(0, (self.bounds.size.height - imageSize.height)/2.0, imageSize.width, imageSize.height)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
        self.addTarget(self, action: "showUnlight:", forControlEvents: UIControlEvents.AllTouchEvents)
    }
    func showUnlight(button:UIButton){
         self.highlighted = false
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
class MyButton:UIButton{
    var imageSize:CGSize = CGSizeZero
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        let centerPoint = CGPointMake(bounds.size.width/2.0, bounds.size.height/2.0)
        return CGRectMake(0, centerPoint.y+3,self.bounds.size.width,20)
    }
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        let centerPoint = CGPointMake(bounds.size.width/2.0, bounds.size.height/2.0)
        return CGRectMake((self.bounds.size.width - imageSize.width)/2.0, centerPoint.y-imageSize.height+2, imageSize.width, imageSize.height)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
        self.addTarget(self, action: "showUnlight:", forControlEvents: UIControlEvents.AllTouchEvents)
    }
    func showUnlight(button:UIButton){
        self.highlighted = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}